﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Ads;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using OmnipotentFlashlight.Model;

namespace OmnipotentFlashlight
{
    public partial class SoftDetail : PhoneApplicationPage
    {
        ProgressIndicator _progress = new ProgressIndicator() { IsVisible = false, IsIndeterminate = true };
        string appid = "";
        AdsModel ad = new AdsModel();
        public SoftDetail()
        {
            InitializeComponent();
            this.SetValue(SystemTray.OpacityProperty, 0.00001);
            Loaded += new RoutedEventHandler(SoftDetail_Loaded);
        }

        void SoftDetail_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = _progress;
            _progress.IsVisible = true;
            appid = NavigationContext.QueryString["appid"];
            App.ads.OnGetAdDetailRequest += new Ads.AdsHelper.GetAdsDetail(ads_OnGetAdDetailRequest);
            App.ads.GetDetail(appid);
        }

        void ads_OnGetAdDetailRequest(AdsModel _ad)
        {
            if (ad == null)
            {
                s_loading1.Text = "网络错误，请返回。";
                s_loading2.Text = "网络错误，请返回。";
                _progress.IsVisible = false;
                return;
            }
            ad = _ad;
            s_img.Source = new BitmapImage(new Uri(ad.imgUrl, UriKind.Absolute));
            s_title.Text = ad.title;
            s_desc.Text = ad.text.Replace("<br>", " \n").Replace("<br/>", "\n"); ;
            s_size.Text = ad.size + "MB";
            s_sores.Text = ad.scores.ToString() + "分";
            SetStar();
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true;
            s_loading1.Visibility = Visibility.Collapsed;
            g_content.Visibility = Visibility.Visible;

            if (ad.screen.Count == 0)
            {
                s_loading2.Text = "暂无截图";
            }
            else
            {
                s_loading2.Visibility = Visibility.Collapsed;
                g_content2.Visibility = Visibility.Visible;
                list.ItemsSource = ad.screen;
            }
            _progress.IsVisible = false;
            App.ads.OnGetAdDetailRequest -= ads_OnGetAdDetailRequest;
        }


        void SetStar()
        {
            ImageSource img = new BitmapImage(new Uri("icon/Star-g.png", UriKind.Relative));
            if (ad.scores <= 1 && ad.scores >= 0)
            {
                s2.Source = img; s3.Source = img; s4.Source = img; s5.Source = img;
            }
            else if (ad.scores <= 2 && ad.scores > 1)
            {
                s3.Source = img; s4.Source = img; s5.Source = img;
            }
            else if (ad.scores <= 3 && ad.scores > 1)
            {
                s4.Source = img; s5.Source = img;
            }
            else if (ad.scores <= 4 && ad.scores > 1)
            {
                s5.Source = img;
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (ad.url.Length == 36 && ad.url.Substring(8, 1) == "-")
            {
                MarketplaceDetailTask detail = new MarketplaceDetailTask();
                detail.ContentIdentifier = ad.url;
                detail.ContentType = MarketplaceContentType.Applications;
                detail.Show();
            }
            else
            {
                new WebBrowserTask() { Uri = new Uri(ad.url, UriKind.Absolute) }.Show();
            }
        }

        private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (list.SelectedIndex >= 0)
            {
                List<BitmapImage> lst = new List<BitmapImage>();
                foreach (object o in list.Items)
                {
                    var myListBoxItem =
    (FrameworkElement)(list.ItemContainerGenerator.ContainerFromItem(o));
                    Image im = Common.FindVisualChild<Image>(myListBoxItem);
                    lst.Add((BitmapImage)im.Source);
                }
                Arg arg = new Arg() { list = lst, selectIndex = list.SelectedIndex };
                App.obj = (Object)arg;
                NavigationService.Navigate(new Uri("/SoftDetail_Screen.xaml", UriKind.Relative));
            }
        }



        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            list.SelectedIndex = -1;
            base.OnNavigatedFrom(e);
        }

    }
    public class Arg
    {
        public int selectIndex { get; set; }
        public List<BitmapImage> list { get; set; }
    }
}
